package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.AlsCancelDetailLogEntity;
import com.taxshare.seller.entity.AlsCancelLogEntity;
import com.taxshare.seller.mapper.AlsCancelLogMapper;
import com.taxshare.seller.view.dto.InvoicedDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: SellerInvoiceCancelLogService
 * @Description: ALS驳回日志实体
 * @author: hjh
 * @date: 2021/6/21 9:39
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class AlsCancelLogService extends ServiceImpl<AlsCancelLogMapper, AlsCancelLogEntity> {

    @Autowired
    private AlsCancelDetailLogService alsCancelDetailLogService;

    /**
     * 查询待开已作废发票
     *
     * @param query
     * @return
     */
    public Response<IPage<AlsCancelLogEntity>> getInvoiceCancelByPage(PageRequest<InvoicedDTO> query) {
        QueryWrapper<AlsCancelLogEntity> queryWrapper = new QueryWrapper<>();
        InvoicedDTO invoicedDTO = query.getInfo();
        if (StrUtil.isNotBlank(invoicedDTO.getInvoiceType())) {
            queryWrapper.eq("invoice_type", invoicedDTO.getInvoiceType());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getMakeInvoiceType())) {
            queryWrapper.eq("make_invoice_type", invoicedDTO.getMakeInvoiceType());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getInvoiceCode())) {
            queryWrapper.eq("invoice_code", invoicedDTO.getOldInvoiceCode());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getInvoiceNo())) {
            queryWrapper.eq("invoice_no", invoicedDTO.getInvoiceNo());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getInvoiceState())) {
            queryWrapper.eq("invoice_state", invoicedDTO.getInvoiceState());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getOldInvoiceCode())) {
            queryWrapper.eq("old_invoice_code", invoicedDTO.getOldInvoiceNo());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getOldInvoiceNo())) {
            queryWrapper.eq("old_invoice_no", invoicedDTO.getOldInvoiceNo());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getSellerName())) {
            queryWrapper.like("seller_name", invoicedDTO.getSellerName());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getSellerTaxNo())) {
            queryWrapper.eq("seller_tax_no", invoicedDTO.getSellerTaxNo());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getPurchaserName())) {
            queryWrapper.like("purchaser_name", invoicedDTO.getPurchaserName());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getPurchaserTaxNo())) {
            queryWrapper.eq("purchaser_tax_no", invoicedDTO.getPurchaserTaxNo());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getDataSource())) {
            queryWrapper.eq("data_source", invoicedDTO.getDataSource());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getDrawer())) {
            queryWrapper.like("drawer", invoicedDTO.getDrawer());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getDocId())) {
            queryWrapper.like("doc_id", invoicedDTO.getDocId());
        }
        if (StrUtil.isNotBlank(invoicedDTO.getInvoiceDateStart()) && StrUtil.isNotBlank(invoicedDTO.getInvoiceDateEnd())) {
            queryWrapper.between("invoice_date", invoicedDTO.getInvoiceDateStart(), invoicedDTO.getInvoiceDateEnd());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<AlsCancelLogEntity> sellerInvoiceCancelLogEntityIPage = PageHelp.siamplePage(this, queryWrapper, query.getPage(), AlsCancelLogEntity.class);
        return Response.success(sellerInvoiceCancelLogEntityIPage);
    }

    /**
     * 代开已作废发票明细查询
     *
     * @param serialNo
     * @return
     */
    public Response<List<AlsCancelDetailLogEntity>> queryCancelDetail(String serialNo) {
        if(StrUtil.isBlank(serialNo)){
            return Response.error("SEL_030");
        }
        QueryWrapper<AlsCancelDetailLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serial_no",serialNo);
        List<AlsCancelDetailLogEntity> list = alsCancelDetailLogService.list(queryWrapper);
        return Response.success(list);
    }
}
